package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.QuerySubscribeActivityBean;
import com.ymatou.iapi.livebuyer.service.QuerySubscribeActivityCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*批量查询是否订阅直播秒杀活动
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QuerySubscribeActivity{
	private static QuerySubscribeActivityBean querysubscribeactivityBean;
	private static QuerySubscribeActivityCall querysubscribeactivityCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int sellerId2 = Integer.parseInt(EnvSetup.getData("SellerId2"));
	LiveRoomWapper LiveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询是否订阅直播秒杀活动");
	}

	@Before
	public void caseUp() {
		querysubscribeactivityBean=new QuerySubscribeActivityBean();
		querysubscribeactivityCall=new QuerySubscribeActivityCall();
		LiveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querysubscribeactivityCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySubscribeActivity_001() {
		Logger.start(true,"批量查询是否订阅直播秒杀活动查询成功");
		try {
			int userId=1;
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveRoomCallService.Subscribeactivity(userId, createlive.getId(), liveacctivityid,productId);
			querysubscribeactivityBean.setLiveActivityIdList(Arrays.asList(liveacctivityid));
			querysubscribeactivityBean.setUserId(userId);
			querysubscribeactivityCall.setData(querysubscribeactivityBean);
			querysubscribeactivityCall.callService();			
			Logger.verifyEquals("200", querysubscribeactivityCall.getString("code"), "验证code");
			List<Integer> data=new ArrayList<Integer>();
			querysubscribeactivityCall.getJsonObject().get("data").getAsJsonArray().forEach(ja->data.add(ja.getAsInt()));
			Logger.verifyEquals(true, data.contains(liveacctivityid), "验证包含liveacctivityid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySubscribeActivity_002() {
		Logger.start(true,"查询多个活动成功");
		try {
			int userId=1;
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveRoomCallService.Subscribeactivity(userId, createlive.getId(), liveacctivityid,productId);
			
			
			AddSellerProductRequest addpro2 = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId2);
			String productId2 = ProductManagerCallServiceV2.addNewProduct(addpro2).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive2 = LiveRoomCallService.initCreatesellerliveBean(sellerId2);
			Date now2 = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime2 = new Date(now.getTime() + 10 * 1000);
			Date endtime2 = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime2, YMTDateUtil.YMDHMS));
			LiveRoom createlive2 = LiveRoomCallService.CreateSellerLive(clive2);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive2.getId(), sellerId2, productId2);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper2 = new YmtCatalogsIWapper();
			List<String> cids2 = CIWapper2.selectCatalogsByProductId(productId2).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean2 = LiveRoomCallService.initCreateLiveActivityBean(createlive2.getId(), cids2.get(0), 5d, 6, starttime2);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean2);
			LiveRoomCallService.SellerLiveOnline(createlive2.getId(), sellerId2);
			Integer liveacctivityid2 = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean2.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveRoomCallService.Subscribeactivity(userId, createlive2.getId(), liveacctivityid2,productId2);
			
			
			querysubscribeactivityBean.setLiveActivityIdList(Arrays.asList(liveacctivityid,liveacctivityid2,1));
			querysubscribeactivityBean.setUserId(userId);
			querysubscribeactivityCall.setData(querysubscribeactivityBean);
			querysubscribeactivityCall.callService();			
			Logger.verifyEquals("200", querysubscribeactivityCall.getString("code"), "验证code");
			List<Integer> data=new ArrayList<Integer>();
			querysubscribeactivityCall.getJsonObject().get("data").getAsJsonArray().forEach(ja->data.add(ja.getAsInt()));
			Logger.verifyEquals(true, data.contains(liveacctivityid), "验证包含liveacctivityid");
			Logger.verifyEquals(true, data.contains(liveacctivityid2), "验证包含liveacctivityid2");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySubscribeActivity_003() {
		Logger.start(true,"不输入liveActivityIdList失败");
		try {
			querysubscribeactivityBean.setUserId(1);
			querysubscribeactivityCall.setData(querysubscribeactivityBean);
			querysubscribeactivityCall.callService();			
			Logger.verifyEquals("100", querysubscribeactivityCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySubscribeActivity_004() {
		Logger.start(true,"不输入userid失败");
		try {
//			querysubscribeactivityBean.setUserId(1);
			querysubscribeactivityBean.setLiveActivityIdList(Arrays.asList(1));
			querysubscribeactivityCall.setData(querysubscribeactivityBean);
			querysubscribeactivityCall.callService();			
			Logger.verifyEquals("100", querysubscribeactivityCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}